<!-- 字典数据 -->
<script lang="ts">
export default {
  name: 'dictData'
};
</script>

<script setup lang="ts">
import {
  getDictPage,
  addDict,
  updateDict,
  deleteListDict
} from '@/api/dict';
import { DictPageVO, DictForm, DictQuery } from '@/api/dict/types';
import { statusOptionList } from '@/option'

const props = defineProps({
  parentId: {
    type: Number,
  },
});

watch(
  () => props.parentId,
  (newVal: number | undefined) => {
    queryParams.parentId = newVal;
    resetQuery();
  }
);

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<DictQuery>({
  current: 1,
  pageSize: 9999,
  parentId: props.parentId
});

const dictList = ref<DictPageVO[]>();

const dialog = reactive<DialogOption>({
  visible: false
});
let formData = reactive<any>({
  status: 1,
  parentId: props.parentId,
});

const rules = reactive({
  label: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
  value: [{ required: true, message: '请输入字典值', trigger: 'blur' }]
});

/**
 * 查询
 */
function handleQuery() {
  if (queryParams.parentId) {
    loading.value = true;
    getDictPage(queryParams)
      .then((res: any) => {
        dictList.value = res.data.data;
        total.value = res.data.total;
      })
      .finally(() => (loading.value = false));
  }
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.current = 1;
  handleQuery();
}

/**
 * 行checkbox change事件
 *
 * @param selection
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/**
 * 打开字典表单弹窗
 *
 * @param row 字典对象
 */
function openDialog(row?: object) {
  dialog.visible = true;
  if (row) {
    dialog.title = '修改字典';
    Object.assign(formData, row);
  } else {
    dialog.title = '新增字典';
  }
}

/**
 * 字典表单提交
 */
function handleSubmit() {
  loading.value = false;
  dataFormRef.value.validate((isValid: boolean) => {
    if (isValid) {
      const dictId = formData.id;
      if (dictId) {
        updateDict(formData)
          .then(() => {
            ElMessage.success('修改成功');
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addDict(formData)
          .then(() => {
            ElMessage.success('新增成功');
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/**
 * 关闭弹窗
 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData = reactive<DictForm>({
    status: 1,
    parentId: props.parentId,
  });

}

/**
 * 删除字典
 */
function handleDelete(id?: number) {
  let idList = ids.value
  id ? idList = [...ids.value, id] : null
  if (idList.length === 0) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteListDict(idList).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search">

      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="name">
          <el-input v-model="queryParams.name" placeholder="字典名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="openDialog()">
          <i-ep-plus />新增
        </el-button>
        <el-button type="danger" :disabled="ids.length === 0" @click="handleDelete()">
          <i-ep-delete />删除
        </el-button>
      </template>


      <el-table :data="dictList" v-loading="loading" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" />
        <el-table-column label="字典名称" prop="label" />
        <el-table-column label="字典值" prop="value" />
        <el-table-column label="状态" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.status == 1" type="success">启用</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="openDialog(scope.row)">
              <i-ep-edit />编辑
            </el-button>
            <el-button type="primary" link @click.stop="handleDelete(scope.row.id)">
              <i-ep-delete />删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 
            <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.current"
              v-model:limit="queryParams.pageSize" @pagination="handleQuery" /> -->
    </el-card>

    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" @close="closeDialog">
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="字典名称" prop="label">
          <el-input v-model="formData.label" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典值" prop="value">
          <el-input v-model="formData.value" placeholder="字典值" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio v-for="item in statusOptionList" :label="item.value" :key="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.description" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
